/**
 * 
 */
package com.game.connection.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.game.connection.message.BulletMessage;
import com.game.connection.message.Message;
import com.game.entity.Bullet;
import com.game.entity.Ship;

/**
 * @author gustavo
 * 
 */
public class ClientPlayerListener implements Runnable {

	private Integer playerId;

	private BufferedReader in;
	private Socket socket;

	private HashMap<Integer, Ship> ships;

	public ClientPlayerListener(Integer id, Socket socket,
			HashMap<Integer, Ship> ships) {

		playerId = id;
		this.socket = socket;
		this.ships = ships;

		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void receive() throws IOException {
		String msg = in.readLine();
		if (msg != null && msg.trim().length() > 6) {
			Message msgt = new Message(playerId, msg);
			if (msgt.getName().equals("ServerCreateNewPlayer")) {
				System.out.println(msgt.getId() + "::::::::::::"
						+ msgt.getName());
				ships.put(new Integer(msgt.getId()), new Ship());
				
			}

			if (ships.containsKey(msgt.getId())) {
				System.out.println(msgt.getId() + "::::::::::::"
						+ msgt.getName());
				ships.get(msgt.getId()).setId(msgt.getId());
				ships.get(msgt.getId()).setX(msgt.getX());
				ships.get(msgt.getId()).setY(msgt.getY());
				ships.get(msgt.getId()).setFaceAngel(msgt.getFaceAngle());
				ships.get(msgt.getId()).setVelX(msgt.getVelX());
				ships.get(msgt.getId()).setVelY(msgt.getVelY());
				ships.get(msgt.getId()).setId(msgt.getId());
				if (ships.get(msgt.getId()).getMessage().size() == 4) {
					ships.get(msgt.getId()).getMessage().remove(0);
				}
				if (msgt.getMessage().trim().length() > 0)
					ships.get(msgt.getId()).getMessage().add(msgt.getMessage());

				int i = 0;

				for (BulletMessage bullet : msgt.getBullets()) {
					ships.get(msgt.getId()).getBullets().get(i)
							.setFaceAngel(bullet.getFaceAngle());
					ships.get(msgt.getId()).getBullets().get(i)
							.setX(bullet.getX());
					ships.get(msgt.getId()).getBullets().get(i)
							.setY(bullet.getY());
					ships.get(msgt.getId()).getBullets().get(i)
							.setVelY(bullet.getVelY());
					ships.get(msgt.getId()).getBullets().get(i)
							.setVelX(bullet.getVelX());
					ships.get(msgt.getId()).getBullets().get(i)
							.setAlive(bullet.isAlive());
					i++;
				}

			}
			// ship.setX(msgt.getX());
			// ship.setY(msgt.getY());
			// ship.setFaceAngel(msgt.getFaceAngle());
			// ship.setVelX(msgt.getVelX());
			// ship.setVelY(msgt.getVelY());
			// ship.setId(msgt.getId());
			// if (ship.getMessage().size() == 4) {
			// ship.getMessage().remove(0);
			// }
			// if (msgt.getMessage().trim().length()>0)
			// ship.getMessage().add(msgt.getMessage());
			//
			//
			// int i = 0;
			//
			// for(BulletMessage bullet: msgt.getBullets()){
			// ship.getBullets().get(i).setFaceAngel(bullet.getFaceAngle());
			// ship.getBullets().get(i).setX(bullet.getX());
			// ship.getBullets().get(i).setY(bullet.getY());
			// ship.getBullets().get(i).setVelY(bullet.getVelY());
			// ship.getBullets().get(i).setVelX(bullet.getVelX());
			// ship.getBullets().get(i).setAlive(bullet.isAlive());
			// i++;
			// }

		}
	}

	@Override
	public void run() {
		while (true) {
			try {
				receive();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}